/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fullhappy.dao;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.fullhappy.entity.MusicFileType;

/**
 *
 * @author hiep
 */
public class MusicFileTypeFacade extends AbstractFacade<MusicFileType> {
    public MusicFileTypeFacade() {
        super(MusicFileType.class);
    }

    public MusicFileType findByExtensionOrCreateNewIfNotExist(String extension) {
        MusicFileType musicFileType = findByExtension(extension);
        if(musicFileType == null){
            musicFileType = new MusicFileType(null, extension, extension);
            try {
                create(musicFileType);
            } catch (Exception ex) {
                Logger.getLogger(MusicFileTypeFacade.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return musicFileType;
    }
    
    public MusicFileType findByExtension(String extension) {
        EntityManager em = getEntityManager();
    Query q = em.createQuery ("SELECT x FROM MusicFileType x WHERE x.extension = '"+extension+"' ");
    List<MusicFileType> results = q.getResultList ();
    return results != null && !results.isEmpty()? results.get(0):null;
    }
    
}
